package com.tedu.api.d4_io.in;

import java.io.*;

/**
 本类测试字符流的读取
 instantiated---实例化
 */
public class Test6_Reader {
    public static void main(String[] args) {
        //method1();//普通字符流的读取
        method2();//高效字符流的读取

    }
    /**用于测试普通字符流的读取*/
    public static void method1() {
        Reader in=null;
        //Reader in=new Reader();Reader是字符读取流的抽象父类
        try {
            in = new FileReader(new File("D:\\ready\\4.txt"));
            //System.out.println(in.read());只读取一次!!!-in,read()
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**用于测试高效字符流---BufferedReader的读取
     * 底层维护了一个char[]，数组长度是8*1024=8192字节=8k
     * */
    public static void method2() {
        /*
         1.创建流对象
         2.读取数据
         3.释放资源
         */
        Reader in2=null;
        try {
            in2=new BufferedReader(new FileReader("D:\\ready\\4.txt"));
            int b;
            while ((b=in2.read())!=-1){
                System.out.println(b);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in2.close();//释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
